﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using DG.Tweening;
using SevenShine;
using System;

public class Level7BeginEnd : MonoBehaviour {

    public static Level7BeginEnd Instance;
    public Text text;
    public InputField inputField;
    public Text inputText;
    public GameObject DeathBirth;
    public GameObject birth;
    public AudioClip RunClip;
    //public AudioClip TypeWriting;
    public AudioClip PassClip;
    public AudioClip[] DialogClip;

    private GameObject dialogObj;
    private Text dialogText;
    private string[] tip = new string[]{"好像不对。","它的名字究竟是什么？","你快醒醒啊！"};
    private AudioSource audios;

	// Use this for initialization
	void Start ()
    {
        Instance = this;
        audios = Camera.main.GetComponent<AudioSource>();

        Begin();
	}
	
    void Begin()
    {
        //各个携程的嵌套可以更换
        StartCoroutine(Run(() =>
        {
            StartCoroutine(Introduce(() =>
        {
            StartCoroutine(Dialog(() =>
            {
               
            }));
        }));
        }));
    }

    public void EndGame()
    {
        //各个携程的嵌套可以更换
        StartCoroutine(IntroduceEnd1(() =>
        {
            StartCoroutine(IntroduceEnd2(() =>
            {
                //StartCoroutine(Dialog(() =>
                //{
                //    Debug.Log("Animation Finish!");
                //}));
            }));
        }));
    }

    IEnumerator IntroduceEnd1(Action action = null)
    {
        yield return new WaitForSeconds(0);
        text.gameObject.SetActive(true);
        //text.text = "";

        //audios.clip = TypeWriting;
        //audios.Play();
        //audios.loop = true;

        //text.DOText(LanguageMgr.GetInstance().ShowText("Level7_Panbai2"),2).SetEase(Ease.Linear).OnComplete(()=> 
        //{
        //    audios.Stop();
        //    text.gameObject.SetActive(false);
        //    if (action != null)
        //    {
        //        action();
        //    }
        //});
        text.text = LanguageMgr.GetInstance().ShowText("Level7_Panbai2");
        yield return new WaitForSeconds(2);
        text.gameObject.SetActive(false);
        if (action != null)
        {
            action();
        }
    }

    IEnumerator IntroduceEnd2(Action action = null)
    {
        yield return new WaitForSeconds(1);
        dialogObj.SetActive(true);

        audios.clip = DialogClip[4];
        audios.Play();
        float l = DialogClip[4].length;

        dialogText.text = "";
        dialogText.DOText(LanguageMgr.GetInstance().ShowText("Level7_Dialog2"), l).SetEase(Ease.Linear).OnComplete(() =>
        {
            dialogObj.SetActive(false);
            //下一关
            audios.clip = PassClip;
            audios.Play();
            audios.loop = false;

            if (action != null)
            {
                action();
            }
        }); ;
    }

    IEnumerator Run(Action action = null)
    {
        yield return new WaitForSeconds(0);
        Animator ani = GameObject.Find("Role_XiaoQi").GetComponent<Animator>();
        ani.SetTrigger("Run");

        audios.clip = RunClip;
        audios.Play();
        audios.loop = true;

        GameObject.Find("XiaoQi").transform.DOMoveX(7.5f,1).SetEase(Ease.Linear).OnComplete(()=> 
        {
            audios.Stop();
            ani.SetTrigger("Idle");
            GameObject.Find("Ground04").GetComponent<Animator>().enabled = true;
            if (action != null)
            {
                action();
            }
        });
       
    }

    IEnumerator Introduce(Action action=null)
    {
        yield return new WaitForSeconds(12);

        //audios.clip = TypeWriting;
        //audios.Play();
        //audios.loop = true;

        //text.DOText(LanguageMgr.GetInstance().ShowText("Level7_Panbai1"),2).SetEase(Ease.Linear).OnComplete(()=> 
        //{
        //    audios.Stop();
        //    text.gameObject.SetActive(false);
        //    if (action != null)
        //    {
        //        action();
        //    }
        //});

        text.text = LanguageMgr.GetInstance().ShowText("Level7_Panbai1");
        yield return new WaitForSeconds(2);
        text.gameObject.SetActive(false);
        if (action != null)
        {
            action();
        }
    }

    IEnumerator Dialog(Action action = null)
    {
        yield return new WaitForSeconds(1);
        dialogObj = Instantiate(Resources.Load("Prefabs/UI/DialogImage"), transform) as GameObject;
        dialogText = dialogObj.GetComponentInChildren<Text>();

        audios.clip = DialogClip[0];
        audios.Play();
        audios.loop = false;
        float l = DialogClip.Length;

        dialogText.DOText(LanguageMgr.GetInstance().ShowText("Level7_Dialog1"), l-1).SetEase(Ease.Linear).OnComplete(()=> 
        {
            dialogObj.SetActive(false);
            inputField.gameObject.SetActive(true);
        });
    }

    public void FinishInput()
    {
        if (inputText.text=="海星鸟")
        {
            Debug.Log("苏醒");
            StartCoroutine("Correct");
        }
        else
        {
            Debug.Log("错了");
            StartCoroutine("Faulse");   
        }
    }

    IEnumerator Correct()
    {
        yield return new WaitForSeconds(1);
        inputField.gameObject.SetActive(false);
        DeathBirth.SetActive(false);
        birth.SetActive(true);
    }

    IEnumerator Faulse()
    {
        float length = 0;
        dialogText.text = "";
        int a = UnityEngine.Random.Range(0,3);
        dialogObj.SetActive(true);
        if (a == 0)
        {
            audios.clip = DialogClip[2];
            audios.Play();
            length = DialogClip[2].length;
        }
        else if (a == 1)
        {
            audios.clip = DialogClip[3];
            audios.Play();
            length = DialogClip[3].length;
        }
        else if (a == 2)
        {
            audios.clip = DialogClip[1];
            audios.Play();
            length = DialogClip[1].length;
        }
        dialogText.DOText(tip[a], length).SetEase(Ease.Linear);

        yield return new WaitForSeconds(length);
        dialogObj.SetActive(false);
    }
}
